from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS# 处理跨域问题
from datetime import datetime

db = SQLAlchemy()# 数据库
cors = CORS() # 跨域

# 数据集模型
class Dataset(db.Model):
    __tableName__ = 'dataset'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True, nullable=False)
    description = db.Column(db.Text)
    path = db.Column(db.String(200), nullable=False)
    train_samples = db.Column(db.Integer, default=0)# 训练样本数
    val_samples = db.Column(db.Integer, default=0) # 验证样本数
    num_classes = db.Column(db.Integer, default=0)  # 新增类别数
    yaml_path = db.Column(db.String(200))          # 新增YAML路径
    status = db.Column(db.String(20), default='unverified') # 状态：unverified/verified/invalid
    created_time = db.Column(db.DateTime, default=datetime.now)

# “模型”模型
class Model(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    dataset_id = db.Column(db.Integer, db.ForeignKey('dataset.id'))
    model_type = db.Column(db.String(50))
    status = db.Column(db.String(20), default='pending')
    created_time = db.Column(db.DateTime, default=datetime.utcnow)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'dataset_id': self.dataset_id,
            # 'dataset': self.dataset.name,#需要根据id从数据集表中获取名称
            'model_type': self.model_type,
            'status': self.status,
            'created_time': self.created_time.isoformat()
        }